<?php

/** @package codymvc */

/**
 * @class View
 * @brief Responsible for outputting to the user.
 *
 * Displays a named template.  The name to use is returned from the Command after
 * it has been run.
 */

class View {

    /**
     * Outputs a named file to the browser.
     * If the named file is not found, throws an exception.
     * @param $template (String) The path to the file to display, relative to
     *          the full path in the 'template_dir' config option.
     * @return null
     */
    static function display($template) {
        $filename = CodyMVC::get_config('template_dir').'/'.$template;
        if ( ! is_file($filename) ) {
            if ( $template == CodyMVC::get_config('error_template') ) {
                // avoid loop
                echo 'An error has occurred, but the error template '.$template.' could not be found.';
                return;
            }
            throw new Exception('File '.$filename.' was not found.');
        }
        readfile( CodyMVC::get_config('template_dir').'/'.$template );
    }

    /**
     * Does a header redirect to a specified URL.
     * @param $url (String) The URL to redirect to.
     * @return null
     */
    static function redirect($url) {
        header('Location: '.$url);
    }

}

?>
